Imports Phoenix.DataAccess
Imports Phoenix.Framework
Imports Phoenix.Helpers

Imports ComponentGo.Calendars


Namespace Forms.Area

    Public Class ScheduleArea : Inherits CalendarAreaBase

        Private mList As New ArrayList
        Private currPoint As New Point

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            Calendar = ResourceScheduler
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents ResourceScheduler As ComponentGo.Calendars.ResourceScheduler
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.ResourceScheduler = New ComponentGo.Calendars.ResourceScheduler
            Me.SuspendLayout()
            '
            'ResourceScheduler
            '
            Me.ResourceScheduler.AllowOverlap = False
            Me.ResourceScheduler.DateFont = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
            Me.ResourceScheduler.DateFormat = "dddd d MMMM yyyy"
            Me.ResourceScheduler.DayLineColor = System.Drawing.Color.FromArgb(CType(215, Byte), CType(177, Byte), CType(0, Byte))
            Me.ResourceScheduler.Dock = System.Windows.Forms.DockStyle.Fill
            Me.ResourceScheduler.EnableEditInPlace = False
            Me.ResourceScheduler.ExtraBreakHourColor = System.Drawing.Color.Fuchsia
            Me.ResourceScheduler.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
            Me.ResourceScheduler.FreeDay = ComponentGo.Calendars.FreeDays.None
            Me.ResourceScheduler.FreeHourColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(238, Byte), CType(158, Byte))
            Me.ResourceScheduler.HoursResolution = ComponentGo.Calendars.HoursResolutions.TenMinutes
            Me.ResourceScheduler.Location = New System.Drawing.Point(0, 0)
            Me.ResourceScheduler.Name = "ResourceScheduler"
            Me.ResourceScheduler.Size = New System.Drawing.Size(360, 448)
            Me.ResourceScheduler.TabIndex = 0
            Me.ResourceScheduler.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            Me.ResourceScheduler.WorkHourBegin = System.TimeSpan.Parse("00:00:00")
            Me.ResourceScheduler.WorkHourEnd = System.TimeSpan.Parse("00:00:00")
            '
            'ScheduleArea
            '
            Me.Controls.Add(Me.ResourceScheduler)
            Me.FirstDate = New Date(2005, 8, 10, 9, 57, 54, 535)
            Me.Name = "ScheduleArea"
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Aux "
        Public Shared Function GetNewAppointmentText(ByVal text As String, ByVal width As Integer, ByVal g As Graphics) As String
            Return "New"
        End Function

        Public Shared Function GetAppointmentText(ByVal text As String, ByVal width As Integer, ByVal g As Graphics) As String
            If g.MeasureString(text, New Font("Arial", 9)).Width < width Then
                Return text
            End If

            Dim i, max As Integer
            max = 0
            For i = 1 To text.Length
                If g.MeasureString(text.Substring(0, i) + "...", New Font("Arial", 9)).Width < width Then
                    max = i
                End If
            Next
            If max = 0 Then
                Return ""
            End If
            Return text.Substring(0, max) + "..."
            '            Return text.Substring(0, count - 2) + "..."
        End Function


        Private Function GetPriceByMasterLevel(ByVal serviceID As Long, ByVal level As Long) As Integer
            Dim row As DataSetData.Service = AppDataLayer.Services.FindByServiceID(serviceID)
            For Each sp As DataSetData.ServiceParam In row.GetServiceParams
                If sp.MasterLevelID = level Then
                    Return sp.Price
                End If
            Next
            Return 0
        End Function

        Private Function GetDurationByMasterLevel(ByVal serviceID As Long, ByVal level As Long) As Integer
            Dim row As DataSetData.Service = AppDataLayer.Services.FindByServiceID(serviceID)
            For Each sp As DataSetData.ServiceParam In row.GetServiceParams
                If sp.MasterLevelID = level Then
                    Return sp.Duration
                End If
            Next
            Return 0
        End Function



        Private Sub CopyCalendarAppointment2DBAppointment(ByVal app As Appointment, ByVal dbApp As DataSetAppointments.Appointment)
            'If dbApp.AppointmentID = 0 Then dbApp.AppointmentID = app.AppointmentID
            'dbApp.ClientID = app.ClientID
            'dbApp.Comment = app.Comment
            'dbApp.CreationTime = app.CreationTime
            'dbApp.Description = app.Description
            'dbApp.ExpectedDuration = app.ExpectedDuration
            'dbApp.FinishTime = app.DateEnd
            'dbApp.OperatorID = app.OperatorID
            'dbApp.PerformerID = app.PerformerID
            'dbApp.ServiceCost = app.ServiceCost
            'dbApp.ServiceID = app.ServiceID
            'dbApp.StartTime = app.DateBegin
            'dbApp.Status = app.Status
        End Sub

#End Region

#Region " Time Navigation "
        Public Overrides Function SetDate(ByVal d As Date) As Date
            If d.Date = d Then
                d = d.AddHours(8)
            End If
            ResourceScheduler.FirstDateTime = d
            Return ResourceScheduler.FirstDateTime
        End Function

        Public Overrides Function SetNext() As Date
            Return SetDate(ResourceScheduler.FirstDateTime.AddDays(1))
        End Function

        Public Overrides Function SetPrev() As Date
            Return SetDate(ResourceScheduler.FirstDateTime.AddDays(-1))
        End Function
        Public Overrides Function SetToday() As Date
            Return SetDate(Date.Today)
        End Function

#End Region

#Region " ToolTip "
        Protected Overrides Function GetCellToolTip(ByVal p As Point) As String
            Dim dt As DateTime = Calendar.HitDateTimeTest(p)
            If dt <> DateTime.MinValue Then
                Return dt.ToString("t")
            Else
                Return Nothing
            End If
        End Function
#End Region


        Public Overrides Property FirstDate() As Date
            Get
                Return ResourceScheduler.FirstDateTime
            End Get
            Set(ByVal Value As Date)
                ResourceScheduler.FirstDateTime = Value
            End Set
        End Property

        Public Overrides ReadOnly Property HasSchedule() As Boolean
            Get
                Return True
            End Get
        End Property


#Region " Open Employee Form "
        Private Sub ResourceScheduler_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ResourceScheduler.MouseDown
            currPoint = New Point(e.X, e.Y)

            If e.Clicks < 2 Then Return
            If e.Button <> MouseButtons.Left Then Return

            If ResourceScheduler.HitTest(currPoint) = ResourceScheduler.Regions.ResourcesLabel Then
                Dim ri As Integer = ResourceScheduler.HitResourceTest(currPoint)
                If (ri >= 0) And (ri < ResourceScheduler.Resources.Count) Then
                    Dim r As Resource = ResourceScheduler.Resources(ri)
                    If Not r Is Nothing Then
                        EmployeeForm.OpenForm(r.Tag)
                    End If
                End If
            ElseIf ResourceScheduler.HitAppointmentTest(currPoint) Is Nothing Then
                Dim app As DataSetAppointments.Appointment = Helpers.AppointmentHelper.NewAppointment(ResourceScheduler.HitDateTimeTest(currPoint))
                Dim ind As Long = ResourceScheduler.HitResourceTest(currPoint)
                If ind < 0 Or ind >= ResourceScheduler.Resources.Count Then Exit Sub
                Dim res As Resource = ResourceScheduler.Resources(ind)
                Dim empl As DataSetData.Employee = res.Tag
                app.PerformerID = empl.EmployeeID
                AppointmentForm.OpenForm(app)
            End If
        End Sub
#End Region

        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If
            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.Copy(obj)
                item.StartTime = ResourceScheduler.HitDateTimeTest(currPoint)  'CurrentDate
                item.PerformerID = ResourceScheduler.Resources(ResourceScheduler.HitResourceTest(currPoint)).Tag.EmployeeID
                Dim m As Integer = item.ExpectedDuration Mod 60
                Dim h As Double = item.ExpectedDuration / 60 - m / 60
                item.FinishTime = item.StartTime.AddHours(h)
                item.FinishTime = item.FinishTime.AddMinutes(m)
                If Helpers.AppointmentHelper.IsValidTime(item.StartTime, item.FinishTime, item) Then
                    Helpers.AppointmentHelper.UpdateAppointment(item, Me)
                Else
                    AppointmentForm.OpenForm(item)
                End If
            Next
        End Sub

        Private Sub ResourceScheduler_FirstDateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResourceScheduler.FirstDateChanged
            OnFirstDateChanged(New DateRangeEventArgs(ResourceScheduler.FirstDateTime, ResourceScheduler.FirstDateTime))
        End Sub

        Private Sub ResourceScheduler_PaintAppointment(ByVal sender As Object, ByVal args As ComponentGo.Calendars.PaintAppointmentEventArgs) Handles ResourceScheduler.PaintAppointment
            If args.Appointment.Tag Is Nothing Then
                Dim rect As Rectangle = Calendar.GetAppointmentTextRectangle(args.Appointment, True)
                If rect.Equals(Rectangle.Empty) Then Return
                args.Graphics.FillRectangle(New SolidBrush(args.Appointment.BackColor), rect)
                CalendarTools.PaintClock(args.Graphics, args.Appointment.DateBegin, rect.Left + 1, rect.Top + 1, rect.Height - 4)

                Dim font As Font = args.Appointment.Font
                If font Is Nothing Then font = Calendar.Font

                Dim sf As StringFormat = New StringFormat
                sf.Trimming = StringTrimming.EllipsisCharacter
                sf.FormatFlags = StringFormatFlags.LineLimit

                Dim trect As RectangleF
                trect.X = rect.X + rect.Height
                trect.Y = rect.Y + 1
                trect.Width = rect.Width - rect.Height
                trect.Height = rect.Height - 1


                args.Graphics.DrawString(args.Appointment.Text, font, New SolidBrush(args.Appointment.ForeColor), trect, sf)

                args.UserPaint = True
            End If
        End Sub

        Protected Overrides Sub OnVisibleChanged(ByVal e As System.EventArgs)

        End Sub
    End Class


End Namespace
